package com.ruoyi.project.gzbook.userinfo.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.web.domain.Ztree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gzbook.userinfo.domain.DgUserInfo;
import com.ruoyi.project.gzbook.userinfo.service.IDgUserInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author mhg
 * @date 2021-10-18
 */
@Controller
@RequestMapping("/gzbook/userinfo")
public class DgUserInfoController extends BaseController
{
    private String prefix = "gzbook/userinfo";

    @Autowired
    private IDgUserInfoService dgUserInfoService;

    @RequiresPermissions("gzbook:userinfo:view")
    @GetMapping()
    public String userinfo()
    {
        return prefix + "/userinfo";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("gzbook:userinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DgUserInfo dgUserInfo)
    {
        startPage();
        List<DgUserInfo> list = dgUserInfoService.selectDgUserInfoList(dgUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("gzbook:userinfo:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DgUserInfo dgUserInfo)
    {
        List<DgUserInfo> list = dgUserInfoService.selectDgUserInfoList(dgUserInfo);
        ExcelUtil<DgUserInfo> util = new ExcelUtil<DgUserInfo>(DgUserInfo.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("gzbook:userinfo:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DgUserInfo dgUserInfo)
    {
        return toAjax(dgUserInfoService.insertDgUserInfo(dgUserInfo));
    }

    /**
     * 修改用户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DgUserInfo dgUserInfo = dgUserInfoService.selectDgUserInfoById(id);
        mmap.put("dgUserInfo", dgUserInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("gzbook:userinfo:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DgUserInfo dgUserInfo)
    {
        return toAjax(dgUserInfoService.updateDgUserInfo(dgUserInfo));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("gzbook:userinfo:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dgUserInfoService.deleteDgUserInfoByIds(ids));
    }


    /**
     * 加载所有用户列表树
     */
    @RequiresPermissions("gzbook:userinfo:list")
    @GetMapping("/treeUser")
    @ResponseBody
    public List<Ztree> publishData()
    {
        DgUserInfo query = new DgUserInfo();
        List<DgUserInfo> list = dgUserInfoService.selectDgUserInfoList(query);
        List<Ztree> ztrees = new ArrayList<>();
        for (DgUserInfo pojo : list){
            Ztree ztree = new Ztree();
            ztree.setId(pojo.getId());
            ztree.setName(pojo.getUsername()+ "&nbsp;&nbsp;&nbsp;" );
            ztree.setTitle(pojo.getUsername());
            ztree.setpId(null);
            ztree.setChecked(false);
            ztree.setNocheck(false);
            ztree.setOpen(false);
            ztrees.add(ztree);
        }
        logger.info("用户集合为--：" + ztrees);
        return ztrees;
    }



    /**
     * 选择用户
     */
    @RequiresPermissions("gzbook:userinfo:view")
    @GetMapping("/selectUser/{userId}")
    public String selectMenuTree( @PathVariable("userId") Long userId, ModelMap mmap)
    {
        logger.info("传过来的参数为userId--："+ userId);
        DgUserInfo pojo = dgUserInfoService.selectDgUserInfoById(userId);
        logger.info("查询结果--："+ pojo);
        mmap.put("user", pojo);
        return prefix + "/tree";
    }

}
